home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0162_Print from WinWord with DDE.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  2.7 KB  |  109 lines

  1. {
  2. This answer involves opening an existing Word file that has a bookmark already saved.  This code will select and replace the text at the bookmark with our own text, and then it will print.
  3. Note:  The ExecuteMacro() commands are separated into different buttons because of a timing issue.  If you want everything to work from the same procedure, change the TRUE to FALSE.
  4. }
  5. unit Unit1;
  6. interface
  7. uses
  8.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  9.   Forms, Dialogs, DdeMan, StdCtrls;
  10. type
  11.   TForm1 = class(TForm)
  12.     Button1: TButton;
  13.     DCC: TDdeClientConv;
  14.     Button2: TButton;
  15.     Button3: TButton;
  16.     Button4: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure Button2Click(Sender: TObject);
  19.     procedure Button3Click(Sender: TObject);
  20.     procedure Button4Click(Sender: TObject);
  21.   private
  22.     { Private declarations }
  23.   public
  24.     { Public declarations }
  25.   end;
  26. var
  27.   Form1: TForm1;
  28. implementation
  29. {$R *.DFM}
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. var
  32.   P: PChar;
  33. begin
  34.   WinExec('e:\winapps\winword\winword.exe', sw_ShowNormal);
  35.   with DCC do begin
  36.     SetLink('winword', '');
  37.     if not OpenLink then
  38.       ShowMessage('Link not established')
  39.     else
  40.       ExecuteMacro('[FileOpen("c:\temp\foobar.doc")]', True);
  41.   end;
  42. end;
  43. procedure TForm1.Button2Click(Sender: TObject);
  44. begin
  45.   DCC.ExecuteMacro('[EditGoTo("TheBookmarkName")]', True);
  46. end;
  47. procedure TForm1.Button3Click(Sender: TObject);
  48. begin
  49.   DCC.ExecuteMacro('[Insert("This is the new text that is inserted.")]', True);
  50. end;
  51. procedure TForm1.Button4Click(Sender: TObject);
  52. begin
  53.   DCC.ExecuteMacro('[FilePrint]', True);
  54. end;
  55. end.
  56. {****************************************************}
  57. object Form1: TForm1
  58.   Left = 202
  59.   Top = 102
  60.   Width = 403
  61.   Height = 89
  62.   Caption = 'Form1'
  63.   Font.Color = clWindowText
  64.   Font.Height = -13
  65.   Font.Name = 'System'
  66.   Font.Style = []
  67.   PixelsPerInch = 96
  68.   TextHeight = 16
  69.   object Button1: TButton
  70.     Left = 8
  71.     Top = 16
  72.     Width = 89
  73.     Height = 33
  74.     Caption = 'Open'
  75.     TabOrder = 0
  76.     OnClick = Button1Click
  77.   end
  78.   object Button2: TButton
  79.     Left = 104
  80.     Top = 16
  81.     Width = 89
  82.     Height = 33
  83.     Caption = 'Find Bkmk'
  84.     TabOrder = 1
  85.     OnClick = Button2Click
  86.   end
  87.   object Button3: TButton
  88.     Left = 200
  89.     Top = 16
  90.     Width = 89
  91.     Height = 33
  92.     Caption = 'Replace'
  93.     TabOrder = 2
  94.     OnClick = Button3Click
  95.   end
  96.   object Button4: TButton
  97.     Left = 296
  98.     Top = 16
  99.     Width = 89
  100.     Height = 33
  101.     Caption = 'Print'
  102.     TabOrder = 3
  103.     OnClick = Button4Click
  104.   end
  105.   object DCC: TDdeClientConv
  106.     ConnectMode = ddeManual
  107.   end
  108. end
  109.